%{
import java.io.*;
%}

/* YACC Declarations */
%token IDENTIFIER
%token NAME
%token NUMBER
%token STRING
%token UNFORMATTED_STRING


/* Tag Tokens: Opening tag: T_X = "<X".
   Closing tag:  CT_X = "</X>" */
%token T_NUDROID
%token CT_NUDROID
%token T_NAVIGATION
%token CT_NAVIGATION
%token T_STYLE
%token CT_STYLE
%token T_PAGE
%token CT_PAGE
%token T_TEXT
%token CT_TEXT
%token T_INPUT
%token CT_INPUT
%token T_LINK
%token CT_LINK
%token T_IMAGE
%token CT_IMAGE
%token T_LIST
%token CT_LIST
%token T_TABLE
%token CT_TABLE
%token T_AUDIO
%token CT_AUDIO
%token T_VIDEO
%token CT_VIDEO
%token T_BUTTON
%token CT_BUTTON
%token T_FUNCTION
%token CT_FUNCTION
%token T_VAR
%token CT_VAR
%token T_GOTO
%token T_SETVAR
%token T_GETVAR
%token T_GETINPUT
%token T_GETTEXT
%token T_SETTEXT
%token T_LOOP
%token CT_LOOP
%token T_TEST
%token CT_TEST
%token T_TRUE
%token CT_TRUE
%token T_FALSE
%token CT_FALSE

//Boolean literals
%token B_TRUE
%token B_FALSE

//Parameters
%token P_NAME
%token P_PAGEID
%token P_URL
%token P_EMAIL
%token P_BULLET_STYLE
%token P_TYPE
%token P_COLS
%token P_FUNCTIONID
%token P_TYPE
%token P_VALUE
%token P_CONDITION
%token P_START
%token P_END
%token P_ID
%token P_LABEL
%token P_STYLEID
%token P_POSITION
%token P_LENGTH
%token P_WIDTH
%token P_LOCATION
%token P_SIZE
%token P_COLOR
%token P_FONT
%token P_LORIENTATION

//Style Parameters
%token PS_SIZE
%token PS_FONT
%token PS_COLOR
%token PS_PADDING
%token PS_BACKGROUND_COLOR
%token PS_FONT_STYLE
%token PS_WIDTH
%token PS_HEIGHT
%token PS_ALIGN

/* Condition operator associativity */
%left "||"
%left "&&"
%left "!"

/* Grammar follows */
%%

/* APP */

app: T_NUDROID app_name ">" body CT_NUDROID
{
	System.out.println("Reducing app");

	/*$$.sval = "import android.app.Activity;\n" +
	"import android.os.Bundle;\n" +
	"import android.widget.*;\n" +
	
	"public class " + $2.sval + " extends Activity\n" + 
	"{\n" +
		"\t// Default layout\n" +
    "\tRelativeLayout nudroidLayout;" +
    "Intent intent;\n" +
    "int autoID = 1;\n\n";*/
			
		/*@Override
		public void onCreate(Bundle savedInstanceState) 
		{
				super.onCreate(savedInstanceState);
			
        // To create default layout
        nudroidLayout = new RelativeLayout(this);
              
        $4.sval;
        
        // To show views
        setContentView(nudroidLayout);
		}
	}
	//'"';*/
}
;

body: /* Empty String*/ {System.out.println("Empty body");}
   | body upper_level_tag 
   ;

upper_level_tag: page
   | navigation
   | variable
   | style
;


app_name: P_NAME NAME {System.out.println("Name");}
;

/* NAVIGATION */
navigation: T_NAVIGATION tabs CT_NAVIGATION
;

tabs: /* Empty String */
   | tabs tab
;

tab: link
;

/* STYLE */
style: T_STYLE id ">" style_body CT_STYLE
;

style_body: /* Empty String */
   | style_body style_param
;

style_param: style_size
   | style_font
   | style_color
   | style_padding
   | style_bg
   | style_font_style
   | style_width
   | style_height
   | style_align
;

style_size: PS_SIZE NUMBER
;

style_font: PS_FONT font_name
;

style_color: PS_COLOR NAME
;

style_padding: PS_PADDING NUMBER
;

style_bg: PS_BACKGROUND_COLOR NAME
;

style_font_style: PS_FONT_STYLE NAME
;

style_width: PS_WIDTH NUMBER
;

style_height: PS_HEIGHT NUMBER
;

style_align: PS_ALIGN NAME
;

font_name: "serif"
   | "sans"
   | "mono"
;

/* PAGE */

page: T_PAGE id page_params ">" page_body CT_PAGE
{
				// To define layout param
       /* RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lp1.addRule(RelativeLayout.CENTER_HORIZONTAL);
        pageTextView.setLayoutParams(lp1);
        
        // To create page text
        TextView $1.sval;
        pageTextView.setText($2.sval);
        nudroidLayout.addView($1.sval);
        
        pageTextView = new TextView(this);
        pageTextView.setId(autoID++);*/
}
;

page_body: /* Empty String */
   | page_body body_element
;

body_element: text
   | input
   | image
   | list
   | table
   | button
   | action
   | function
   | audio
   | video
;

page_params: /* Empty String */
   | page_params page_param
;

page_param: label
   | style_p
;

/* TEXT OBJECT */

text: T_TEXT id text_params ">" formatted_string CT_TEXT
{
	// To create sample text
	/*TextView $1.sval = new TextView(this);
	$1.setText($3.sval);
	nudroidLayout.addView($1.sval);*/
}
;

text_params: /* Empty String */
   | text_params text_param
;

text_param: style_p
;

/* INPUT OBJECT */
input: T_INPUT id input_params ">" UNFORMATTED_STRING CT_INPUT
;

input_params: /* Empty String */
   | input_params input_param
;

input_param: style_p
;

/* LINK */
link: T_LINK optional_id ref link_params ">" UNFORMATTED_STRING CT_LINK
   | T_LINK optional_id ref link_params "/>"
;

ref: url
   | page_id
   | email
;

link_params: /* Empty String */
   | link_params link_param
;

link_param: style_p
;

page_id: P_PAGEID NAME
;

url: P_URL STRING
;

email: P_EMAIL STRING
;

/* IMAGE */
image: T_IMAGE image_params ">" formatted_string CT_IMAGE
   | T_IMAGE image_params "/>"
;

image_params: /* Empty String */
   | image_params image_param
;

image_param: length
   | width
   | location
   | url
   | label_style
   | label_orientation
   | position
;

/* LIST */
list: T_LIST list_params ">" list_items CT_LIST
;

list_params: /* Empty String */
   | list_params list_param
;

list_param: style_p
   | bullet
   | type
;

list_items: /* Empty String */
   | list_items list_item
;

list_item: text
   | image
   | link
;

bullet: P_BULLET_STYLE bullet_type
;

type: P_TYPE list_type
;

bullet_type: "numbers"
   | "AtoZ"
   | "atoz"
   | "circles"
   | "square"
   | "disc"
;

list_type: "vertical"
   | "horizontal"
;

/* TABLE */
table: T_TABLE id table_params ">" table_items CT_TABLE
;

table_params: /* Empty String */
   | table_params table_param
;

table_param: style_p
   | cols
   | label
   | label_style
   | label_orientation
;

cols: P_COLS NUMBER
;

table_items: /* Empty String */
   | table_items table_item
;

table_item: text
   | link
   | image
;

/* AUDIO */
audio: T_AUDIO id audio_params ">" formatted_string CT_AUDIO
;

audio_params: /* Empty String */
   | audio_params audio_param
;

audio_param: size
   | url
   | location
   | label_style
   | label_orientation
;

/* VIDEO */
video: T_VIDEO id video_params ">" formatted_string CT_VIDEO
;

video_params: /* Empty String */
   | video_params video_param
;

video_param: size
   | url
   | location
   | label_style
   | label_orientation
;

/* BUTTON OBJECT */
button: T_BUTTON id button_params ">" UNFORMATTED_STRING CT_BUTTON
;

button_params: /* Empty String */
   | button_params button_param
;

button_param: style_p
   | function_id
   | label_style
   | label_orientation
;

function_id: P_FUNCTIONID NAME
;

/* VARIABLE */
variable: T_VAR id type_p value ">"
   | T_VAR id type_p ">"
;

type_p: P_TYPE type
;

type: "number"
   | "string"
;

value: P_VALUE expression
   | P_VALUE STRING
; /* Note: type-checking not done in grammar with this construction. */

/* FUNCTION */
function: T_FUNCTION id ">" code CT_FUNCTION
;

/* CODE */
code: action
   | code action
;

action: variable
   | goto
   | setvar
   | getvar
   | getinput
   | gettext
   | settext
   | test
   | loop
;

goto: T_GOTO page_id ">"
;

setvar: T_SETVAR id value ">"
;

getvar: T_GETVAR id ">"
;

getinput: T_GETINPUT id ">"
;

gettext: T_GETTEXT id ">"
;

settext: T_GETTEXT id value ">"
;

/* EXPRESSIONS */
expression: expression '+' term
   | expression '-' term
   | term
;

term: term '*' factor
   | term '/' factor
   | factor
;

factor: '(' expression ')'
   | num
;

num: NUMBER
   | getvar
   | getinput
   | IDENTIFIER
;

/* CONDITIONS */
condition: condition "||" condition
   | condition "&&" condition
   | '!' condition
   | expression relop expression
   | B_TRUE
   | B_FALSE
;

relop: "<"
   | ">"
   | "=="
   | ">="
   | "<="
;

/* TEST */
test: T_TEST P_CONDITION condition ">" true_code false_code CT_TEST
;

true_code: T_TRUE code CT_TRUE
;

false_code: T_FALSE code CT_FALSE
;

/* LOOP */
loop: T_LOOP P_START expression P_END expression ">" code CT_LOOP
;

/* PARAMETERS */

id: P_ID NAME
;

optional_id: /* Empty String */
   | id
;

label: P_LABEL STRING
;

style_p: P_STYLEID NAME
;

position: P_POSITION position_type
;

position_type: "top"
   | "bottom"
   | "left"
   | "right"
   | "center"
;

size: width
   | length
;

length: P_LENGTH NUMBER
;

width: P_WIDTH NUMBER
;

location: P_LOCATION STRING
;

label_style: font_size
   | color
   | font
;

font_size: P_SIZE NUMBER
;

color: P_COLOR STRING
;

font: P_FONT STRING

label_orientation: P_LORIENTATION orientation_type
;

orientation_type: "vertical"
   | "horizontal"
;


/* Strings */

formatted_string: /* Empty String */
   | formatted_string UNFORMATTED_STRING
   | formatted_string link
   | formatted_string getvar
   | formatted_string getinput
   | formatted_string gettext
;

%%

private Yylex lexer;

// Implemented Read and Save function
// Not Implemented make and copy function
public static void main(String args[])
{
	  
    
	/*StringBuilder nudroidSoucre = new StringBuilder();
	Boolean check = false;
	
	// To make default folders for Android SDK
	check = makeFolder();
	
	// To make default android XML layout
	check = makeXMLLayout();
	
	// To make default android XML list
	check = makeXMLList();
	
	// To copy default files such icon.png
	check = copyFile();
	
	// Input Src name
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	System.out.print("Type nudroid src name: ");
	try
	{
		String srcFile = br.readLine();
		nudroidSoucre = readFile(srcFile);		
	}
	catch (IOException e)
	{
		e.getMessage();
		System.out.println("File buffer error: " + e);
	}*/
	
	Parser yyparser;
	
	try {
		yyparser = new Parser(new FileReader(args[0]));
		 yyparser.yyparse();  
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
	
	//check = saveFile(nudroidSoucre);
}

public static StringBuilder readFile(String srcFile)
{
	StringBuilder nudroidSoucre = new StringBuilder();
	
	try
	{
		// File open
		File dirs = new File(srcFile);
		String path = dirs.getAbsolutePath();
		File realFile = new File(path);			
		FileReader fr=new FileReader(realFile);
		BufferedReader br = new BufferedReader(fr);	
	  	  
	  String str="";
  	
  	while ((str=br.readLine())!=null)
  	{
  		nudroidSoucre.append(str);
  	}
	  			
		return nudroidSoucre;
	}
	catch (Exception e)
	{
		e.getMessage();
		System.out.println("File open error: " + e);
		
		return null;
	}
}

public static Boolean saveFile(StringBuilder nudroidSoucre)
{
	return false;
	// File open to save dest code
	// Save destSource to file - it should be saved at src/package struct (i.e /src/com/nudroid/)
	//try
	//{
//		//TODO - Giving compilation errors.
		///*File dirs = new File(nudroidSoucre);
		//String path = System.getProperty("user.dir") + "\\dest.java";
		//FileWriter fw = new FileWriter(path);
		//fw.write(nudroidSoucre);
		//fw.close();
		//System.out.println("Successed");*/
		//return true;
	//}
	//catch (IOException e)
	//{
//		e.getMessage();
		//System.out.println("File open error: " + e);
//		
		//return false;
	//}
}

public static Boolean makeFolder()
{
	// TODO: make folders
	try
	{
		return true;
	}
	catch (Exception e)
	{
		System.out.println("Err: Make folder - " + e);
		return false;
	}
}

public static Boolean makeXMLLayout()
{
	// TODO: make main XML layout
	try
	{
		return true;
	}
	catch (Exception e)
	{
		System.out.println("Err: Make XML layout - " + e);
		return false;
	}
}

public static Boolean makeXMLList()
{
	// TODO: make list XML layout
	try
	{
		return true;
	}
	catch (Exception e)
	{
		System.out.println("Err: Make list XML layout - " + e);
		return false;
	}
}

public static Boolean copyFile()
{
	// TODO: copy some files to directory that created by makeFolder function
	try
	{
		return true;
	}
	catch (Exception e)
	{
		System.out.println("Err: Copy files - " + e);
		return false;
	}
}

  /* interface to the lexer */
  private int yylex () {
    int yyl_return = -1;
    try {
      yyl_return = lexer.yylex();
    }
    catch (IOException e) {
      System.err.println("IO error :"+e);
    }
    return yyl_return;
  }


void yyerror(String s)
{
	System.err.println("Error: "+s);
}

  /* lexer is created in the constructor */
  public Parser(Reader r) {
    lexer = new Yylex(r, this);
  }

